package com.factory.iamp.service.impl;

import com.factory.iamp.dao.LineMapper;
import com.factory.iamp.dao.RoleMapper;
import com.factory.iamp.dao.UserMapper;
import com.factory.iamp.pojo.User;
import com.factory.iamp.pojo.UserList;
import com.factory.iamp.pojo.UserLogin;
import com.factory.iamp.pojo.UserRegister;
import com.factory.iamp.service.UserService;
import com.factory.iamp.utils.exception.APIException;
import com.factory.iamp.utils.helper.FileUploadHelper;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.helper.ResponseCode;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Throwable.class)
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private LineMapper lineMapper;

    @Override
    public int Count() {
        return userMapper.Count();
    }

    @Override
    public int CountLine(int line) {
        return lineMapper.getMemberCountInLine(line);
    }

    @Override
    public int QueryCount(String QueryStr) {
        return userMapper.QueryCount(QueryStr);
    }

    @Override
    public int findCount(String name) {
        return userMapper.findCount(name);
    }

    @Override
    public int ToDoOrderNums(String uid) {
        return userMapper.ToDoOrderNums(uid);
    }

    @Override
    public int ToDoOrderNumsLine(String uid, int line) {
        return userMapper.ToDoOrderNumsLine(uid, line);
    }

    @Override
    public int CreateOrderNums(String uid) {
        return userMapper.CreateOrderNums(uid);
    }

    @Override
    public int CreateOrderNumsLine(String uid, int line) {
        return userMapper.CreateOrderNumsLine(uid, line);
    }

    @Override
    public int RelateOrderNums(String uid) {
        return userMapper.RelateOrderNums(uid);
    }

    @Override
    public int RelateOrderNumsLine(String uid, int line) {
        return userMapper.RelateOrderNumsLine(uid, line);
    }

    @Override
    public List<UserList> ListPaged(PageHelper data) {
        return userMapper.ListPaged(data.calOffset(), data.getLimit());
    }

    @Override
    public List<UserList> QueryListPaged(PageHelper data, String QueryStr) {
        return userMapper.QueryListPaged(data.calOffset(), data.getLimit(), QueryStr);
    }

    @Override
    public List<UserList> findListPaged(PageHelper data, String name) {
        return userMapper.findListPaged(data.calOffset(), data.getLimit(), name);
    }

    @Override
    public User findById(String uid) {
        return userMapper.findById(uid);
    }

    @Override
    public String Login(UserLogin data) {
        User user = userMapper.findByName(data.getUsername());
        if(user == null || !user.getPassword().equals(data.hashPassword()))
            throw new APIException(ResponseCode.WRONG_PASSWORD, "登录失败，用户名或密码错误");
        userMapper.renewToken(user.getUid(), user.renewToken());
        return user.makeToken();
    }

    @Override
    public String Register(UserRegister data) {
        if(userMapper.findById(data.getUid()) != null)
            throw new APIException(ResponseCode.VALIDATE_FAILED, "注册失败，工号重复");
        if(userMapper.findByName(data.getUsername()) != null)
            throw new APIException(ResponseCode.VALIDATE_FAILED, "注册失败，用户名重复");
        if(roleMapper.findById(data.getRole()) == null)
            throw new APIException(ResponseCode.VALIDATE_FAILED, "注册失败，不存在角色");
        data.setPassword(data.hashPassword());
        userMapper.addUser(data);
        User user = userMapper.findByName(data.getUsername());
        if(user == null) throw new APIException(ResponseCode.DATABASE_FAILED, "注册失败");
        return user.makeToken();
    }

    @Override
    public String uploadAvatar(String uid, MultipartFile file) throws Throwable {
        String filePath = FileUploadHelper.uploadedPath("/data/user/avatar/", file);
        userMapper.updateAvatar(uid, filePath);
        return filePath;
    }

    @Override
    public String uploadSignImg(String uid, MultipartFile file) throws Throwable {
        String filePath = FileUploadHelper.uploadedPath("/data/user/sign/", file);
        userMapper.updateSignImg(uid, filePath);
        return filePath;
    }

    @Override
    public void Logout(User user) {
        userMapper.renewToken(user.getUid(), RandomStringUtils.randomAlphanumeric(16));
    }

    @Override
    public void resetPassword(User user) {
        String password = user.getPassword();
        user = userMapper.findById(user.getUid());
        user.setPassword(password);
        userMapper.resetPassword(user.getUid(), user.hashPassword(), user.renewToken());
    }

    @Override
    public void changePassword(User user, String newPassword) {
        user.setPassword(newPassword);
        userMapper.resetPassword(user.getUid(), user.hashPassword(), user.renewToken());
    }

    @Override
    public void deleteUser(String uid) {
        userMapper.deleteUser(uid);
    }
}